<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\Api\UserRequest;
use App\Http\Requests\Api\VerificationCodeRequest;
use App\Models\User;
use Illuminate\Support\Facades\Cache;


class UsersController extends Controller
{
    public function store(UserRequest $request)
    {
//        $key = 'verificationCode_' . str_random(15);
//        \Cache::put($key, ['phone' => 15267189004, 'code' => '1111'],60 );
//        echo $key;
//        return;
        $verification_key = Cache::get($request->verification_key);
        if (!$verification_key) {
            return $this->response->error('验证码失效', 422);
        }

        if (!hash_equals($verification_key['code'], $request->verification_code)) {
            return $this->response->errorUnauthorized('验证码错误');
        }

        User::create([
            'name'     => $request->name,
            'phone'    => $request->phone,
            'password' => bcrypt($request->password),
        ]);

        Cache::forget($request->verification_key);
        return $this->response->created();

    }
}
